#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname basic $PACKAGE); if (($? != 0)); then exit 1; fi;
PKG_VERSION=$(pkgversion basic $PACKAGE); if (($? != 0)); then exit 2; fi;
PKG_ARCHIVE=$(pkgarchive basic $PACKAGE); if (($? != 0)); then exit 3; fi;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE); if (($? != 0)); then exit 4; fi;
PKG_DEST=/usr/pkg/$PKG_NAME/$PKG_VERSION;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
patch -Np1 -i ../$PKG_ARCHIVE-fhs-1.patch && \
rm -Rfv ../glibc-build && \
mkdir -v ../glibc-build && \
cd ../glibc-build



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
../$PKG_ARCHIVE/configure    \
    --prefix=/usr          \
    --disable-profile      \
    --enable-kernel=2.6.32 \
    --enable-obsolete-rpc \
    && \
    make && \
	make check;
	if (($? != 0)); then
		if [[ $(read -p "Some checks fail... go on [y/n]? " RES && echo $RES | grep -o '^y') != "y" ]]; then
			errcho "User aborted after make check failed";
			exit 8;
		fi
	fi;
	rm -vRf $PKG_DEST/inst && \
    make DESTDIR=$PKG_DEST/inst install && \
    mkdir -pv $PKG_DEST/inst/usr/lib/locale && \
    make DESTDIR=$PKG_DEST/inst localedata/install-locales


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
mkdir -pv $PKG_DEST/inst/etc && \
cp -v ../glibc-2.20/nscd/nscd.conf $PKG_DEST/inst/etc/nscd.conf && \
mkdir -pv $PKG_DEST/inst/var/cache/nscd && \
echo '# Begin /etc/nsswitch.conf

passwd: files
group: files
shadow: files

hosts: files dns
networks: files

protocols: files
services: files
ethers: files
rpc: files

# End /etc/nsswitch.conf' > $PKG_DEST/inst/etc/nsswitch.conf && \
echo '# Begin /etc/ld.so.conf
/usr/local/lib
/opt/lib

# Add an include directory
include /etc/ld.so.conf.d/*.conf
' > $PKG_DEST/inst/etc/ld.so.conf && \
mkdir -pv $PKG_DEST/inst/etc/ld.so.conf.d && \
cp -vf ../tzdata2014g.tar.gz $PKG_DEST && \
cd .. && rm -Rfv glibc-build



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
echo "# Package information file... generated at make process" > $PKG_DEST/pkg.info && \
echo "# pkg name   source archive    version" >> $PKG_DEST/pkg.info && \
echo "$PKG_NAME  $PKG_ARCHIVE  $PKG_VERSION" >> $PKG_DEST/pkg.info && \
if [ -d $LFS/sources/pkg/basic/$PKG_NAME ]; then cp -aRv $LFS/sources/pkg/basic/$PKG_NAME/* $PKG_DEST; fi && \
cp -avf $LFS/sources/pkg/pkgutils.sh $PKG_DEST/pkgutils.sh && \
tar -czvvf /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz -C /usr/pkg/$PKG_NAME $PKG_VERSION && \
rm -Rfv /usr/pkg/$PKG_NAME && \
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
echo "Build complete basic/$PACKAGE"
echo "Package files are stored to: /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz"
echo " --OK--"
